home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 August / CICA - The Ultimate Collection of Shareware for Windows (Disc 2) (August 1995).iso / disc2 / programr / atre27.exe / ATREE_27 / LFWIN / LFEDITIO.C < prev    next >
C/C++ Source or Header  |  1992-08-01  |  11KB  |  375 lines

  1. /*****************************************************************************
  2.  ****                                                                     ****
  3.  **** lfeditio.c                                                          ****
  4.  ****                                                                     ****
  5.  **** atree release 2.7 for Windows                                       ****
  6.  **** Adaptive Logic Network (ALN) simulation program.                    ****
  7.  **** Copyright (C) A. Dwelly, R. Manderscheid, M. Thomas, W.W. Armstrong ****
  8.  ****               1991, 1992                                            ****
  9.  ****                                                                     ****                                                  ****
  10.  **** License:                                                            ****
  11.  **** A royalty-free license is granted for the use of this software for  ****
  12.  **** NON_COMMERCIAL PURPOSES ONLY. The software may be copied and/or     ****
  13.  **** modified provided this notice appears in its entirety and unchanged ****
  14.  **** in all derived source programs.  Persons modifying the code are     ****
  15.  **** requested to state the date, the changes made and who made them     ****
  16.  **** in the modification history.                                        ****
  17.  ****                                                                     ****
  18.  **** Patent License:                                                     ****
  19.  **** The use of a digital circuit which transmits a signal indicating    ****
  20.  **** heuristic responsibility is protected by U. S. Patent 3,934,231     ****
  21.  **** and others assigned to Dendronic Decisions Limited of Edmonton,     ****
  22.  **** W. W. Armstrong, President.  A royalty-free license is granted      ****
  23.  **** by the company to use this patent for NON_COMMERCIAL PURPOSES to    ****
  24.  **** adapt logic trees using this program and its modifications.         ****
  25.  ****                                                                     ****
  26.  **** Limited Warranty:                                                   ****
  27.  **** This software is provided "as is" without warranty of any kind,     ****
  28.  **** either expressed or implied, including, but not limited to, the     ****
  29.  **** implied warrantees of merchantability and fitness for a particular  ****
  30.  **** purpose.  The entire risk as to the quality and performance of the  ****
  31.  **** program is with the user.  Neither the authors, nor the             ****
  32.  **** University of Alberta, its officers, agents, servants or employees  ****
  33.  **** shall be liable or responsible in any way for any damage to         ****
  34.  **** property or direct personal or consequential injury of any nature   ****
  35.  **** whatsoever that may be suffered or sustained by any licensee, user  ****
  36.  **** or any other party as a consequence of the use or disposition of    ****
  37.  **** this software.                                                      ****
  38.  **** Modification history:                                               ****
  39.  ****                                                                     ****
  40.  **** 91.05.20 Initial implementation, M. Thomas                          ****
  41.  **** 91.07.17 atree v2.0 for Windows, M. Thomas                          ****
  42.  **** 92.04.27 atree v2.5 for Windows, M. Thomas                          ****
  43.  **** 92.03.07 Release 2.6, Monroe Thomas                                 ****
  44.  **** 92.01.08 Release 2.7, Monroe Thomas                                 ****
  45.  ****                                                                     ****
  46.  *****************************************************************************/
  47.  
  48. /***** editor I/O *****/
  49.  
  50. #include <windows.h>
  51. #include <string.h>
  52. #include "lfedit.h"
  53.  
  54. /* in filedlg.c */
  55. int DoFileOpenDlg (HWND, LPSTR);
  56. int DoFileSaveDlg (HWND, LPSTR);
  57. int DoOutFileDlg  (HWND, LPSTR);
  58. LPSTR lstrrchr (LPSTR, char);
  59.  
  60. extern char szAppName[];       /*in lfedit.c*/
  61. extern char szFileNameOut[];
  62. extern char szFileNameIn[];
  63.  
  64. BOOL bUserAbort;
  65. HWND hDlgPrint;
  66.  
  67. #pragma argsused
  68.  
  69. BOOL FAR PASCAL PrintDlgProc(HWND hDlg, WORD message, WORD wParam, LONG lParam)
  70. {
  71.     switch (message)
  72.     {
  73.         case WM_INITDIALOG:
  74.             EnableMenuItem (GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
  75.             return TRUE;
  76.  
  77.     case WM_COMMAND:
  78.       bUserAbort = TRUE;
  79.       EnableWindow(GetParent(hDlg), TRUE);
  80.       DestroyWindow(hDlg);
  81.       hDlgPrint = 0;
  82.       return TRUE;
  83.   }
  84.   return FALSE;
  85. }
  86.  
  87. #pragma argsused
  88.  
  89. BOOL FAR PASCAL AbortProc (HDC hPrinterDC, short nCode)
  90. {
  91.   MSG msg;
  92.  
  93.   while(!bUserAbort && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  94.   {
  95.     if (!hDlgPrint || !IsDialogMessage(hDlgPrint, &msg))
  96.     {
  97.             TranslateMessage(&msg);
  98.       DispatchMessage(&msg);
  99.     }
  100.   }
  101.   return !bUserAbort;
  102. }
  103.  
  104. HDC GetPrinterDC (void)
  105. {
  106.   static char szPrinter[80];
  107.   char *szDevice, *szDriver, *szOutput;
  108.  
  109.   GetProfileString("windows", "device", ",,,", szPrinter, 80);
  110.  
  111. #pragma warn -pia
  112.   if (((szDevice = strtok (szPrinter, ",")) &&
  113.        (szDriver = strtok (NULL     , ", "))) &&
  114.        (szOutput = strtok (NULL     , ", ")))
  115. #pragma warn .pia
  116.   {
  117.         return CreateDC(szDriver, szDevice, szOutput, NULL);
  118.   }
  119.   else return 0;
  120. }
  121.  
  122. BOOL PrintFile (HANDLE hInst, HWND hwnd, HWND hwndEdit, char *szFileName)
  123. {
  124.     BOOL        bError = FALSE;
  125.     char        szMsg[40];
  126.     FARPROC     lpfnAbortProc, lpfnPrintDlgProc;
  127.     HDC         hdcPrn;
  128.     NPSTR       psBuffer;
  129.     short       yChar, nCharsPerLine, nLinesPerPage,
  130.                             nTotalLines, nTotalPages, nPage, nLine, nLineNum = 0;
  131.     int         nEscapeReturn;
  132.     TEXTMETRIC  tm;
  133.  
  134.     if (0 ==
  135.          (nTotalLines = (short) SendMessage(hwndEdit, EM_GETLINECOUNT, 0 , 0L)))
  136.   {
  137.         return FALSE;
  138.   }
  139.  
  140.     if (NULL == (hdcPrn = GetPrinterDC()))
  141.   {
  142.         return TRUE;
  143.   }
  144.  
  145.     /* set printer font */
  146.     GetTextMetrics (hdcPrn, &tm) ;
  147.     yChar = tm.tmHeight  + tm.tmExternalLeading;
  148.  
  149.     nCharsPerLine = GetDeviceCaps (hdcPrn, HORZRES) / tm.tmAveCharWidth;
  150.     nLinesPerPage = GetDeviceCaps (hdcPrn, VERTRES) / yChar;
  151.     nTotalPages   = (nTotalLines + nLinesPerPage - 1) / nLinesPerPage;
  152.  
  153.     psBuffer = (NPSTR) LocalAlloc(LPTR, nCharsPerLine);
  154.  
  155.     EnableWindow(hwnd, FALSE);
  156.  
  157.     bUserAbort = FALSE;
  158.     lpfnPrintDlgProc = MakeProcInstance(PrintDlgProc, hInst);
  159.     hDlgPrint = CreateDialog(hInst, "PrintDlgBox", hwnd, lpfnPrintDlgProc);
  160.     SetDlgItemText (hDlgPrint, IDD_FNAME, szFileName);
  161.  
  162.     lpfnAbortProc = MakeProcInstance (AbortProc, hInst);
  163.     Escape (hdcPrn, SETABORTPROC, 0, (LPSTR)lpfnAbortProc, NULL);
  164.  
  165.     strcat (strcat (strcpy (szMsg, szAppName), " - "), szFileName);
  166.  
  167.     nEscapeReturn = Escape(hdcPrn, STARTDOC, strlen(szMsg), szMsg, NULL);
  168.  
  169.     if (nEscapeReturn < 0)
  170.   {
  171.         bError = TRUE;
  172.   }
  173.     else
  174.     {
  175.         for (nPage = 0; nPage < nTotalPages; nPage++)
  176.         {
  177.             for (nLine = 0; nLine < nLinesPerPage &&
  178.                       nLineNum < nTotalLines; nLine++, nLineNum++)
  179.             {
  180.                 *(short *) psBuffer = nCharsPerLine;
  181.                 TabbedTextOut(hdcPrn, 0, yChar * nLine, psBuffer,
  182.                                             (short) SendMessage (hwndEdit, EM_GETLINE, nLineNum,
  183.                                           (LONG) (LPSTR) psBuffer), 0, NULL, 0);
  184.             }
  185.  
  186.             nEscapeReturn = Escape(hdcPrn, NEWFRAME, 0, NULL, NULL) ;
  187.  
  188.             if (nEscapeReturn < 0)
  189.             {
  190.                 bError = TRUE;
  191.                 break;
  192.             }
  193.  
  194. #pragma warn -rch
  195.             if (bUserAbort)
  196.       {
  197.                 break;
  198.       }
  199. #pragma warn .rch
  200.         }
  201.     }
  202.  
  203.     if (!bError)
  204.   {
  205.         Escape(hdcPrn, ENDDOC, 0, NULL, NULL);
  206.   }
  207.  
  208.     if (!bUserAbort)
  209.     {
  210.         EnableWindow(hwnd, TRUE);
  211.         DestroyWindow(hDlgPrint);
  212.     }
  213.  
  214.     if (bError || bUserAbort)
  215.     {
  216.         strcat(strcpy(szMsg, "Could not print: "), szFileName);
  217.         MessageBox (hwnd, szMsg, szAppName, MB_OK | MB_ICONEXCLAMATION);
  218.     }
  219.  
  220.     LocalFree ((LOCALHANDLE) psBuffer);
  221.     FreeProcInstance (lpfnPrintDlgProc);
  222.     FreeProcInstance (lpfnAbortProc);;
  223.  
  224.     DeleteDC (hdcPrn);
  225.  
  226.     return bError || bUserAbort;
  227. }
  228.  
  229. void OkMessageBox(HWND hwnd, char *szString, char *szFileName)
  230. {
  231.     char szBuffer [256];
  232.   wsprintf (szBuffer, szString, (LPSTR) szFileName);
  233.   MessageBox(hwnd, szBuffer, szAppName, MB_OK | MB_ICONEXCLAMATION);
  234. }
  235.  
  236. long FileLength (HANDLE hFile)
  237. {
  238.     long    lCurrentPos = _llseek(hFile, 0L, 1);
  239.     long    lFileLength = _llseek(hFile, 0L, 2);
  240.  
  241.     _llseek(hFile, lCurrentPos, 0);
  242.  
  243.     return lFileLength;
  244. }
  245.  
  246. BOOL GetOutFile (HWND hwnd)
  247. {
  248.     char szBuffer[256];
  249.  
  250.     lstrcpy(szFileNameOut, szFileNameIn);
  251.  
  252.     *(lstrrchr(szFileNameOut, '.')) = '\0';
  253.  
  254.     lstrcat(szFileNameOut, ".out");
  255.  
  256.     if (!DoOutFileDlg (hwnd, szFileNameOut))
  257.   {
  258.         return FALSE;
  259.   }
  260.  
  261.     return TRUE;
  262. }
  263.  
  264. BOOL ReadFile (HWND hwnd, HWND hwndEdit, POFSTRUCT pof, LPSTR szFileName,
  265.                              BOOL bAskName)
  266. {
  267.   DWORD dwLength;
  268.   HANDLE hFile, hTextBuffer;
  269.     LPSTR lpTextBuffer;
  270.     char szBuffer[80];
  271.   int nResult;
  272.  
  273.   if (bAskName)
  274.   {
  275.         if (!DoFileOpenDlg (hwnd, szFileName))
  276.     {
  277.       return FALSE;
  278.     }
  279.     }
  280.  
  281. #pragma warn -rng
  282.     if (-1 == (hFile = OpenFile(szFileName, pof, OF_READ)))
  283.   {
  284.     OkMessageBox(hwnd, "Cannot open file %s", szFileName);
  285.     return FALSE;
  286.   }
  287.  
  288.     if ((dwLength = FileLength(hFile)) < 48000)
  289.     {
  290.         EnableWindow(hwndEdit, TRUE);
  291.  
  292.         if (NULL == (hTextBuffer = GlobalAlloc(GHND, (DWORD) dwLength + 1)))
  293.       {
  294.         _lclose(hFile);
  295.         OkMessageBox(hwnd,"Not enough memory for %s", szFileName);
  296.         return FALSE;
  297.       }
  298.  
  299.       lpTextBuffer = GlobalLock (hTextBuffer);
  300.       _lread(hFile, lpTextBuffer, (WORD) dwLength);
  301.       _lclose(hFile);
  302.  
  303.       lpTextBuffer [(WORD) dwLength] = '\0';
  304.  
  305.       SetWindowText (hwndEdit, lpTextBuffer);
  306.       GlobalUnlock (hTextBuffer);
  307.         GlobalFree(hTextBuffer);
  308.     }
  309.     else
  310.     {
  311.         _lclose(hFile);
  312.         sprintf(szBuffer, "%s is too large to edit.\n\n"
  313.                                             "Run %s without editing?",
  314.                       szFileName, szFileName);
  315.  
  316.         nResult = MessageBox(hwnd, szBuffer, "LF File too large",
  317.                                              MB_YESNO | MB_ICONQUESTION);    
  318.         if (nResult == IDNO)
  319.         {
  320.             return FALSE;
  321.         }
  322.         else
  323.         {
  324.             PostMessage(hwnd, WM_COMMAND, IDM_RUN, 0L);
  325.             EnableWindow(hwndEdit, FALSE);
  326.     }
  327.   }
  328.  
  329.   return TRUE;
  330. }
  331.  
  332. BOOL WriteFile (HWND hwnd, HWND hwndEdit, POFSTRUCT pof, LPSTR szFileName,
  333.                                 BOOL bAskName)
  334. {
  335.   char szBuffer[40];
  336.   HANDLE hFile, hTextBuffer;
  337.   NPSTR npTextBuffer;
  338.     WORD wStatus, wLength;
  339.  
  340.   if (bAskName)
  341.   {
  342.         if(!DoFileSaveDlg (hwnd, szFileName))
  343.         {
  344.             return(FALSE);
  345.     }
  346.     }
  347.     else
  348.   {
  349.         OpenFile (szFileName, pof, OF_PARSE);
  350.   }
  351.  
  352.   if (-1 == (hFile = OpenFile(szFileName, pof, OF_CREATE)))
  353.   {
  354.     OkMessageBox(hwnd, "Cannot create file %s", szFileName);
  355.     return FALSE;
  356.   }
  357. #pragma warn .rng
  358.  
  359.   wLength = GetWindowTextLength(hwndEdit);
  360.   hTextBuffer = (HANDLE) SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);
  361.   npTextBuffer = LocalLock(hTextBuffer);
  362.  
  363.   if (wLength != _lwrite(hFile, npTextBuffer, wLength))
  364.   {
  365.     _lclose(hFile);
  366.     OkMessageBox(hwnd, "Cannot write file %s to disk: ", szFileName);
  367.     return FALSE;
  368.   }
  369.  
  370.   _lclose(hFile);
  371.   LocalUnlock(hTextBuffer);
  372.  
  373.   return TRUE;
  374. }
  375.